package com.hao.demo.springcloud.server.provider.impl;

import com.hao.demo.springcloud.server.provider.dto.DemoDTO;
import com.hao.demo.springcloud.server.provider.DemoProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class DemoProviderImpl implements DemoProvider {

    @Value("${server.port}")
    private Integer serverPort;


    @Override
    public String queryByName(@NotNull(message = "名称不能为空") String name) {
        // 记录被调用的日志
        log.info("[echo][queryByName 被调用啦 name({})]", name);
        throw new RuntimeException("测试异常");
        //return serverPort + "-provider: " + name;
    }

    @Override
    public String createDemo(DemoDTO demoDTO) {
        log.info("[echo][createDemo 被调用啦 demoDTO({})]", demoDTO);

        return serverPort + "-provider: true";
    }

    @Override
    public String queryDemoDTO(DemoDTO demoDTO) {
        log.info("[echo][queryDemoDTO 被调用啦 demoDTO({})]", demoDTO);
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort + "-provider: " + demoDTO;
    }
}
